package com.pdsu.graduation_design.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.pdsu.graduation_design.R;
import com.pdsu.graduation_design.dto.PayCommodityDTO;
import com.pdsu.graduation_design.listener.PayListItemButtonListener;
import com.pdsu.graduation_design.listener.PayListItemListener;
import com.squareup.picasso.Picasso;

import java.util.List;

public class PayListViewItemListViewAdapter extends BaseAdapter {

    private Context context;
    private View view;
    private CheckBox checkBox;
    private static List<PayCommodityDTO> payCommodityDTOList;
    private LinearLayout layout;
    private TextView all_pr;
    private TextView number;
    private TextView commodity_single_price_tv;
    public PayListViewItemListViewAdapter(Context context, List<PayCommodityDTO> payCommodityDTOList, TextView all_pr) {
        this.context = context;
        this.payCommodityDTOList = payCommodityDTOList;
        this.all_pr = all_pr;
    }
    /**
     * 将数据用list装入
     */
    private TextView commodityName;
    private ImageView commodityImageView;
    Button add_btn,del_btn;
    @Override
    public int getCount() {
        return payCommodityDTOList.size();
    }

    @Override
    public Object getItem(int position) {
        return payCommodityDTOList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //初始化
        view=View.inflate(context,R.layout.fragment_pay_listview_item_listviewitem,null);
        commodityImageView=view.findViewById(R.id.fragment_pay_listview_item_listview_commodityimage);
        commodityName=view.findViewById(R.id.fragment_pay_listview_item_listview_commodityName);

        layout=view.findViewById(R.id.fragment_pay_listview_item_listview_item_detail);
        add_btn=view.findViewById(R.id.fragment_pay_listview_item_listview_add_btn);
        del_btn=view.findViewById(R.id.fragment_pay_listview_item_listview_delete_btn);
        checkBox=view.findViewById(R.id.checkbox_pay);
        number=view.findViewById(R.id.pay_number);
        commodity_single_price_tv=view.findViewById(R.id.fragment_pay_listview_item_listview_commodityprice);
        Picasso.with(context).load(payCommodityDTOList.get(position).getImage()).into(commodityImageView);
        number.setText(payCommodityDTOList.get(position).getCnumber()+"");
       //传入商品id
        commodityName.setText(payCommodityDTOList.get(position).getCname());
        commodity_single_price_tv.setText(payCommodityDTOList.get(position).getPrice()+"");
//        bind(position);
//        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                Double all_p=Double.valueOf(all_pr.getText().toString());
//                Double  pri=payCommodityDTOList.get(position).getPrice();
//                int cnumber=payCommodityDTOList.get(position).getCnumber();
//
//
//                Integer num= Integer.valueOf(number.getText().toString());
//
//                if (isChecked){
//                    //￥ pay_allprice
//                    System.out.println(pri+"p");
//                    System.out.println(cnumber+"c");
//                    all_p = all_p+pri*cnumber;
//                    all_pr.setText(all_p+"");
//                    payCommodityDTOList.get(position).setBo(true);
//                    //总价钱加一
//                }else {
//                    all_p = all_p-pri*cnumber;
//                    payCommodityDTOList.get(position).setBo(false);
//                    all_pr.setText(all_p+"");
//                    //总价减一
//
//                }
//
//
//            }
//        });

        return view;
    }

    //对每个组件添加监听
    private void bind(int pos){


//        layout.setOnClickListener(new PayListItemListener(payCommodityDTOList.get(pos).getName()));
//        add_btn.setOnClickListener(new PayListItemButtonListener(add_btn,del_btn,null,view, payCommodityDTOList.get(pos).getBo(),all_pr, payCommodityDTOList.get(pos)));
//        del_btn.setOnClickListener(new PayListItemButtonListener(add_btn,del_btn,null,view, payCommodityDTOList.get(pos).getBo(),all_pr, payCommodityDTOList.get(pos)));
    }


}

